﻿/// <summary>
/// Author: Le Van Binh
/// Date: 2013/06/07
/// </summary>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Security.Cryptography;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace SketchTour.Models
{
    [MetadataTypeAttribute(typeof(MSUR00Models.MSUR00ModelsMetadata))]
    public partial class MSUR00Models
    {
        #region Variable
        
        
        
        public string Note;
        
        
        
        #endregion

        #region Properties
        public string UserName { get; set; }
        public string Password { get; set; }
        public string ConfirmPassword { get; set; }
        public string FullName { get; set; }
        //public string Note { get; set; }
        public int RoleID { get; set; }
        public string RoleName { get; set; }
        public int IsActive { get; set; }
        public Nullable<System.DateTime> CreateDate { get; set; }
        public string CreateBy { get; set; }
        public Nullable<System.DateTime> UpdateDate { get; set; }
        public string UpdateBy { get; set; }
        #endregion

        #region Encrypt Key
        //Create a variable to control the key, with start key 'm' is the static variable
        protected const string m_EncryptKey = "SketchTour";
        #endregion

        #region Method
        /// <summary>
        /// Function Encrypt the string to MD5 string
        /// </summary>
        /// <param name="toEncrypt"></param>
        /// <param name="useHashing"></param>
        /// <returns></returns>
        /// @cond
        /// @Author : Le Van Binh
        /// @endcond
        public static string Encrypt(string toEncrypt, bool useHashing)
        {
            byte[] keyArray;
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

            //If hashing use get hashcode regards to your key
            if (useHashing)
            {
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(m_EncryptKey));
                //Always release the resources and flush data
                // of the Cryptographic service provide. Best Practice
                hashmd5.Clear();
            }
            else
                keyArray = UTF8Encoding.UTF8.GetBytes(m_EncryptKey);

            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            //set the secret key for the tripleDES algorithm
            tdes.Key = keyArray;
            //mode of operation. there are other 4 modes.
            //We choose ECB(Electronic code Book)
            tdes.Mode = CipherMode.ECB;
            //padding mode(if any extra byte added)

            tdes.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = tdes.CreateEncryptor();
            //transform the specified region of bytes array to resultArray
            byte[] resultArray =
              cTransform.TransformFinalBlock(toEncryptArray, 0,
              toEncryptArray.Length);
            //Release resources held by TripleDes Encryptor
            tdes.Clear();
            //Return the encrypted data into unreadable string format
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }

        //public class RegisterModel
        //{
        //    [Required]
        //    [Display(Name = "User name")]
        //    //public string UserName { get; set; }

        //    [Required]
        //    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
        //    [DataType(DataType.Password)]
        //    [Display(Name = "Password")]
        //    //public string Password { get; set; }

        //    [DataType(DataType.Password)]
        //    [Display(Name = "Confirm password")]
        //    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
        //    //public string ConfirmPassword { get; set; }
        //}

        internal sealed class MSUR00ModelsMetadata
        {
            [Required]
            public string UserName;

            [Required]
            [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
            [DataType(DataType.Password)]         
            public string Password;

            [DataType(DataType.Password)]            
            //[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
            
            [Required]
            public int RoleID;

            [Required]
            public string FullName;
            public int IsActive;
            public Nullable<System.DateTime> CreateDate;
            public string CreateBy;
            public Nullable<System.DateTime> UpdateDate;
            public string UpdateBy;

        }

        public static class MSUR00Key
        {
            public const string username = "UserName";
            public const string password = "Password";
            public const string fullname = "FullName";
            public const string roleid = "RoleID";
            public const string isactive = "IsActive";         
            public const string CreateDate = "CreateDate";
            public const string CreateBy = "CreateBy";
            public const string UpdateDate = "UpdateDate";
            public const string UpdateBy = "UpdateBy";
        }
        #endregion

        public class SimpleObject
        {
            public int Id { get; set; }

            public string StringValue { get; set; }

            public int? NumberValue { get; set; }

            public SimpleObject()
            {

            }

            public SimpleObject(int id, string stringValue, int? numberValue)
            {
                Id = id;
                StringValue = stringValue;
                NumberValue = numberValue;
            }

            public override string ToString()
            {
                return string.Format("{0}|{1}", Id, StringValue);
            }
        }
    }
}